<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;

/**
 * 数据字典控制器
 *
 * @author derek <693472824@qq.com>
 */
class DictionaryController extends BaseController {

    /**
     * 获取类型列表
     *
     * @return 返回列表
     * @author derek <693472824@qq.com>
     */
    public function index() {
        
        $dictionary_list = M('Dictionary')->order('dict_id desc');
        $list = $this->lists($dictionary_list);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 获取子类列表
     *
     * @return 返回列表
     * @author derek <693472824@qq.com>
     */
    public function dictEnumList ()
    {
        $dict_id = I('dictionary_id');
        $enum_id = I('enumID');
        if (empty($dict_id)) {
            $this->error('参数错误');
        }
        $dict = M('Dictionary');
        if (! empty($enum_id)) {
            $date['dict_id']=$dict_id;
            $date['enum_id']=$enum_id;
            $date['_logic']='AND';
            $dictlist = M('DictionaryEnum')->where($date)->order('enum_value desc')->select();
        } else {
            $date['dict_id']=$dict_id;
            $dictlist = M('DictionaryEnum')->where($date)->order('enum_value desc')->select();
        }
        $dictEnumList = M('DictionaryEnum')->where('dict_id='.$dict_id)->order('enum_value desc')->select();
        $dictModel = $dict->where('dict_id=' . $dict_id)->find();
        $this->assign('dict_id', $dict_id);
        $this->assign('dictlist', $dictlist);
        $this->assign('dictModel', $dictModel);
        $this->assign('dictEnumList',$dictEnumList);
        $this->display();
    }

    /**
     * 增加类型以及修改类型的主界面
     *
     * @param integer $dict_id
     *            类型ID.
     * @author derek <693472824@qq.com>
     */
    public function editAction ()
    {
        $dict_id = I('dict_id');
        $dict = D('Dictionary', 'Logic');
        if (! empty($dict_id)) {
            
            $dictModel = $dict->getDicByID($dict_id);
            if (empty($dictModel)) {
                $this->error('获取数据失败', U('Dictionary/index'));
            } else {
                $this->assign('dicModel', $dictModel);
            }
        }
        
        $this->display();
    }

    /**
     * 增加类型
     *
     * @author derek <693472824@qq.com>
     */
    public function addAction ()
    {
        $dictLogic = D('Dictionary', 'Logic');
        $result = $dictLogic->addAction();
        if ($result === false) {
            $this->error('操作失败,'.$dictLogic->getError());
        } else {
            $this->success('操作成功', U('Dictionary/index'));
        }
    }

    /**
     * 增加子类型
     *
     * @author derek <693472824@qq.com>
     */
    public function addEnum ()
    {
        $dictLogic = D('Dictionary', 'Logic');
        $result = $dictLogic->addEnum();
        $dict_id = I('dictEnum_id');
        if (empty($dict_id)) {
            if ($result === false) {
                $this->error('操作失败,' . $dictLogic->getError());
            } else {
                $this->success('操作成功');
            }
        } 

        else {
            
            if ($result === false) {
                 $this->ajaxReturn(0);
            } else {
                 $this->ajaxReturn(1);
            }
        }
    }

    /**
     * 类型删除
     *
     * @param array|integer $dict_id
     *            类型ID
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function del ()
    {
        $dict_id = I('dict_id');
        if (empty($dict_id)) {
            $this->error('参数错误');
        }
        $dictLogic = D('Dictionary', 'Logic');
        $result = $dictLogic->del($dict_id);
        if ($result === false) {
            $this->error('操作失败', U('index'));
        } else {
            $this->success('操作成功！', U('index'));
        }
    }

    /**
     * 子类型删除
     *
     * @param array|integer $dict_id
     *            类型ID
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function delEnum ()
    {
        $dictEnum_id = I('enum_ids');
        if (empty($dictEnum_id)) {
            $this->error('参数错误');
        }
        $dictLogic = D('Dictionary', 'Logic');
        $result = $dictLogic->delEnum($dictEnum_id);
        if ($result === false) {
            $this->error('操作失败');
        } else {
            $this->success('操作成功！');
        }
    }
}